If the answer is true something extra is done. If the answer if false nothing is done. (If you are not hungry you don't buy cookies.)
The "cookie problem" is about whether to do something extra. The choice is about whether to add a visit to the cookie shop to your shopping trip. The following program aids the shopper in this decision. The user of the program rates various factors on a scale of 1 to 10. If the combination of hunger, aroma, and visual appeal of the cookie exceed a threshold of 15, the program recommends a purchase.
import java.io.*; class CookieDecision { public static void main (String[] args) throws IOException { String charData; double hunger, look, smell ; BufferedReader stdin = new BufferedReader ( new InputStreamReader( System.in ) ); System.out.println("How hungry are you (1-10):"); charData = stdin.readLine(); hunger = ( Double.valueOf( charData ) ).doubleValue(); System.out.println("How nice does the cookie look (1-10):"); charData = stdin.readLine(); look = ( Double.valueOf( charData ) ).doubleValue(); System.out.println("How nice does the cookie smell (1-10):"); charData = stdin.readLine(); smell = ( Double.valueOf( charData ) ).doubleValue(); if ( (hunger + look + smell ) > 15.0 ) System.out.println("Buy the cookie!" ); System.out.println("Continue down the Mall."); } } |
This is just like the decisions of the previous chapter, but now there is only a true branch.